//
//  SettingViewController.m
//  AlarmClock
//
//  Created by kai ren on 20/11/2011.
//  Copyright 2011 Renkai. All rights reserved.
//

#import "SettingViewController.h"

@implementation SettingViewController

@synthesize lblSinaUser, lblTencentUser, switchTencent, switchSina;

#define SINA    100
#define TENCENT 101

#pragma mark - 
#pragma mark private

- (void)switchAction:(UISwitch *)sender{
	if (sender == switchSina) {
		if (sender.on) {
			SinaWeiboLogInController *logCtrl = [[SinaWeiboLogInController alloc] initWithDeleget:nil];
			[self presentModalViewController:logCtrl animated:YES];
			[logCtrl release];
		} else {
			deleteFile(SINA_OAUTH_CACHE);
			lblSinaUser.text = @"";
		}
	} else if (sender == switchTencent) {
		if (sender.on) {
			TencentLogInController *logCtrl = [[TencentLogInController alloc] initWithDeleget:nil];
			[self presentModalViewController:logCtrl animated:YES];
			[logCtrl release];
		} else {
			deleteFile(TENCENT_OAUTH_CACHE);
			lblTencentUser.text = @"";
		}
	}
}

#pragma mark -
#pragma mark init

- (id)initWithStyle:(UITableViewStyle)style{
	if (self = [super initWithStyle:style]) {
		self.title = NSLocalizedString(@"Setting", nil);
	}
	
	return self;
}

#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationController.view.backgroundColor = Background;
	self.navigationItem.leftBarButtonItem = [self editButtonItem];
	
	//table
	self.view.backgroundColor = [UIColor clearColor];
	
	//Switch
	
	switchSina = [[UISwitch alloc] init];
	switchSina.center = CGPointMake(320 - switchSina.frame.size.width/2 - 30, 25);
	[switchSina addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

	switchTencent = [[UISwitch alloc] init];
	switchTencent.center = CGPointMake(320 - switchTencent.frame.size.width/2 - 30, 25);
	[switchTencent addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];	
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
	
	//Sina
	NSDictionary *dicSina = [NSDictionary dictionaryWithContentsOfFile:getFileFullPath(SINA_OAUTH_CACHE)];
	
	if ([dicSina objectForKey:USER_ID]) {
		switchSina.on = YES;
	} else {
		switchSina.on = NO;
		lblSinaUser.text = @"";
	}
	
	//Tencent
	NSDictionary *dicTencent = [NSDictionary dictionaryWithContentsOfFile:getFileFullPath(TENCENT_OAUTH_CACHE)];
	
	if ([dicTencent objectForKey:USER_NAME]) {
		switchTencent.on = YES;
		lblTencentUser.text = [dicTencent objectForKey:USER_NICK] ? @"" : [dicTencent objectForKey:USER_NICK];
		
		TencentWeiboController *ctrl = [[TencentWeiboController alloc] init];
		ctrl.delegate = self;
		[ctrl getNick];
	}
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	return NSLocalizedString(@"Weibo Setting", nil);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"id%d%d", indexPath.row, indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                       reuseIdentifier:CellIdentifier] 
                autorelease];
	}
	
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = NSLocalizedString(@"Sina", nil);
            cell.textLabel.font = [UIFont boldSystemFontOfSize:22];
            cell.imageView.image = [UIImage imageNamed:@"Sina.png"];
            lblSinaUser = cell.detailTextLabel;
			
			[cell addSubview:switchSina];
            break;
        case 1:
            cell.textLabel.text = NSLocalizedString(@"Tencent", nil);
            cell.textLabel.font = [UIFont boldSystemFontOfSize:22];
            cell.imageView.image = [UIImage imageNamed:@"Tencent.png"];
			cell.detailTextLabel.text = @"213";
			lblTencentUser = cell.detailTextLabel;
			
			[cell addSubview:switchTencent];
        break;   
    }
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 50;
}

#pragma mark -
#pragma mark WeiboControllerDelegate

- (void)connectionCompleteWithWeibo:(WeiboType)weibo operation:(WeiboOperationType)operation content:(NSString *)content{
	if(weibo == WeiboSina && switchSina.on) {
		NSMutableDictionary *dicSina = [NSMutableDictionary dictionaryWithContentsOfFile:getFileFullPath(SINA_OAUTH_CACHE)];
		[dicSina setObject:content forKey:USER_NICK];
		[dicSina writeToFile:getFileFullPath(SINA_OAUTH_CACHE) atomically:YES];
		lblSinaUser.text = content;
	} else if (weibo = WeiboTencnet && switchTencent.on) {
		NSMutableDictionary *dicTencent = [NSMutableDictionary dictionaryWithContentsOfFile:getFileFullPath(TENCENT_OAUTH_CACHE)];
		[dicTencent setObject:content forKey:USER_NICK];
		[dicTencent writeToFile:getFileFullPath(TENCENT_OAUTH_CACHE) atomically:YES];
		lblTencentUser.text = content;
	}
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[lblSinaUser release];
	[lblTencentUser release];
	[switchTencent release];
	[switchSina release];
	
    [super dealloc];
}

@end

